package foo.bar.wiki.web.action.user;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.UserTypeConverter;
import foo.bar.wiki.domain.User;
import foo.bar.wiki.services.UserService;
import foo.bar.wiki.services.exception.BusinessLogicException;
import foo.bar.wiki.services.exception.BusinessLogicExecution;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class ChangeProfilePasswordActionBean extends BaseActionBean {

    private static final Log LOG = LogFactory.getLog(ChangeProfilePasswordActionBean.class);

    public static final String VIEW = "/user/changeProfilePassword.jsp";


    private User user;

    @Validate(trim=true, required=true, on={"save"})
    private String password;

    @Validate(trim=true, required=true, on={"save"})
    private String confirmPassword;

    private UserService userService;



    @DefaultHandler
    public Resolution view() {
        user = userService.getUserById(getCurrentUser().getId());
        return new ForwardResolution(generateThemePath(VIEW));
    }



    public Resolution save() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution() {
            public Resolution execute() throws Throwable {
                userService.changePassword(user, password);
                getContext().getMessages().add(new SimpleMessage("Password changed successfully"));
                return new RedirectResolution(ChangeProfilePasswordActionBean.class, "view");
            }
        });
    }
    @Before(stages={LifecycleStage.BindingAndValidation}, on={"save"})
    public void before_save() {
        user = userService.getUserById(getCurrentUser().getId());
    }
    @ValidationMethod(on={"save"})
    public void validation_save(ValidationErrors errors) {
        if (password != null && confirmPassword != null) {
            if ((!password.trim().equals(confirmPassword.trim()))) {
                errors.add("password", new SimpleError("password mismatch"));
            }
        }
    }


    @SpringBean("userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
}
